package hanxx.security.dao;


import hanxx.system.dao.BaseDao;
import hanxx.security.domain.SysRole;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.io.Serializable;
import java.util.List;

/**
 * 角色表 数据层
 *
 */
public interface SysRoleDao extends BaseDao<SysRole, Serializable> {


    /**
     * 根据机构ID获取角色选择框列表
     *
     * @param orgId 用户ID
     * @return 角色列表
     */
    @Query(value = " select * from sys_role where del_flag = '0' and org_id =?1 ",nativeQuery = true)
    public List<SysRole> findAllByOrgId(Long orgId);
    /**
     * 根据用户ID查询角色
     *
     * @param userId 用户ID
     * @return 角色列表
     */
    @Query(value = "select distinct r.*  from sys_role r  " +
            " left join sys_user_role ur on ur.role_id = r.role_id  " +
            " left join sys_user u on u.user_id = ur.user_id  " +
            " left join sys_dept d on u.dept_id = d.dept_id " +
            " WHERE r.del_flag = '0' and ur.user_id =?1 ", nativeQuery = true)
    public List<SysRole> selectRolePermissionByUserId(Long userId);


    @Query(value = "select distinct r.*  from sys_role r  " +
            " left join sys_user_role ur on ur.role_id = r.role_id  " +
            " left join sys_user u on u.user_id = ur.user_id  " +
            " left join sys_dept d on u.dept_id = d.dept_id " +
            " WHERE r.del_flag = '0' and ur.user_id =?1 and locate(?2,r.role_key)=0 ", nativeQuery = true)
    public List<SysRole> selectRolePermissionByUserIdAndExcludeType(Long userId,String excludeType);



    /**
     * 根据用户ID获取角色选择框列表
     *
     * @param userId 用户ID
     * @return 选中角色ID列表
     */
    @Query(value = "select r.role_id  " +
            " from sys_role r  " +
            " left join sys_user_role ur on ur.role_id = r.role_id  " +
            " left join sys_user u on u.user_id = ur.user_id  " +
            " where u.user_id = ?1 ", nativeQuery = true)
    public List<Long> selectRoleIdListByUserId(Long userId);


    @Query(value = "select r.*  " +
            " from sys_role r  " +
            " left join sys_user_role ur on ur.role_id = r.role_id  " +
            " left join sys_user u on u.user_id = ur.user_id  " +
            " where u.user_id = ?1 ", nativeQuery = true)
    public List<SysRole> selectRoleListByUserId(Long userId);

    /**
     * 通过角色ID查询角色
     *
     * @param roleId 角色ID
     * @return 角色对象信息
     */
    @Query(value = "select * from sys_role r where r.role_id = ?1 " ,nativeQuery = true)
    public SysRole selectRoleById(Long roleId);

    /**
     * 根据用户ID查询角色
     *
     * @param userName 用户名
     * @return 角色列表
     */
    @Query(value = " select distinct r.* from sys_role r " +
            " left join sys_user_role ur on r.role_id= ur.role_id " +
            " left join sys_user u on ur.user_id=u.user_id " +
            " WHERE r.del_flag = '0' and u.user_name =?1 " ,nativeQuery = true)
    public List<SysRole> selectRolesByUserName(String userName);

    /**
     * 校验角色名称是否唯一
     *
     * @param roleName 角色名称
     * @return 角色信息
     */
    @Query(value = "select * from sys_role r where r.role_name=?1 limit 1",nativeQuery = true)
    public SysRole checkRoleNameUnique(String roleName);

    /**
     * 校验角色权限是否唯一
     *
     * @param roleKey 角色权限
     * @return 角色信息
     */
    @Query(value = "select * from sys_role r where r.role_key=?1 limit 1" ,nativeQuery = true)
    public SysRole checkRoleKeyUnique(String roleKey);




    /**
     * 通过角色ID删除角色
     *
     * @param roleId 角色ID
     * @return 结果
     */
    @Modifying
    @Query(value = " delete from sys_role where role_id=?1 ", nativeQuery = true)
    public int deleteRoleById(Long roleId);

    /**
     * 批量删除角色信息
     *
     * @param roleIds 需要删除的角色ID
     * @return 结果
     */
    @Modifying
    @Query(value = " update sys_role set del_flag = '2' where role_id in (?1 )" ,nativeQuery = true)
    public int deleteRoleByIds(Long[] roleIds);
}
